<?php
  require('../includes/class.main.php');
  $manager = new MeetingManager();
  $lookup = new MeetingLookup();
  $cities = $lookup->getCities();
  $zips = $lookup->getZips();
  $zip = "";
  $city = "";
  $name = "";
  $return = "";
  $display_zip = "";
  $locations = $lookup->getClubhouses();

  if (isset($_POST['submit']) == "Search") {
    $location = trim($_POST['location']);
    $name = trim($_POST['name']);
    $city = $_POST['city'];
    $zip = $_POST['zip'];
    $day = $_POST['day'];
    $time = $_POST['time'];
    $meridian = $_POST['meridian'];
    $codes = $_POST['codes'];
  }

  if (isset($_POST['simple']) == "Search") {
    //$name = $_POST['name'];	// ??? RMF disabled
    //$day = $_POST['day'];		// ??? RMF disabled
    //$time = $_POST['time'];	// ??? RMF disabled
    $terms = $_POST['terms'];

    $data = $lookup->parseTerms($terms);
    $city = strtoupper(($data[0]));
    $zip = $data[1];
  }

  if ($city == "CITY") { $city = ""; }
  
  include('../template/admin-header.php');
  
?>		

<div class="left">
<?php 
include('menu.php');
?>
</div>

<div class="right">
	<h2><span class="blue">To change or delete a meeting, locate it by searching, then click the '<img src="../images/edit_icon_small.png" border="0" alt="edit" />' to its left.</span></h2>
  	
	<form method="post">
 		<div class="adminForm" >
		    <span class="label col1">Group</span>
		    <input type="text" name="name" size="40" value="<?php echo $name; ?>">
		    <br>
   			
   			<span class="label col1">Location</span>
			<select name="location">
        	<?php
	          if ($location != "") {
	            if ($location == "%") {
	              $display_location = "";
	            } else {
	              $display_location = $location;
	            }
	            echo "<option value='$location' selected>$display_location</option>";
	          }
	        ?>
    	  	<?php echo $locations; ?>
			</select>
     		<br>
		    
		    <span class="label col1">City</span>
		    <select name="city">
		        <?php
		          if ($city != "") {
		            if ($city == "%") {
		              $display_city = "";
		            } else {
		              $display_city = $city;
		            }
		            echo "<option value='$city' selected>$display_city</option>";
		          }
		        ?>
		      	<?php echo $cities; ?>
		    </select>

		    <?php
		        if ($zip != "") {
		            if ($zip == "%") {
		              $display_zip = "";
		            } else {
		              $display_zip = $zip;
		            }
			  	}
			?>
		    <span class="label">Zip</span>
		    <input type="text" name="zip" size="5" value="<?php echo $display_zip; ?>"/>
		    <br>

    		<span class="label col1">Day</span>
		    <select name="day">
				<?php
				  if ($day != "") {
			            if (preg_match("/%/",$day)) {
			              $display_day = "";
			            } else {
			              $display_day = $day;
				    }
				    echo "<option value='$day' selected>$display_day</option>";
				  }
				?>
			      <option value="%"></option>
			      <option>SUNDAY</option>
			      <option>MONDAY</option>
			      <option>TUESDAY</option>
			      <option>WEDNESDAY</option>
			      <option>THURSDAY</option>
			      <option>FRIDAY</option>
			      <option>SATURDAY</option>
		    </select>
    		<span class="label">Time</span>
    		<select name="time">
				<?php
				  if ($time != "") {
				    if(!preg_match("/:00$/",$time)) {
				      $display_time = $time . ":00";
				    }
				    if (preg_match("/%/",$time)) {
				      $display_time = "";
				    }
				    echo "<option value='$time' selected>$display_time</option>";
				  }
				  if ($time == "%") {
				    $display_time = "";
				  }
				?>
		      <option value="%"></option>
		      <option value="1">1:00</option>
		      <option value="2">2:00</option>
		      <option value="3">3:00</option>
		      <option value="4">4:00</option>
		      <option value="5">5:00</option>
		      <option value="6">6:00</option>
		      <option value="7">7:00</option>
		      <option value="8">8:00</option>
		      <option value="9">9:00</option>
		      <option value="10">10:00</option>
		      <option value="11">11:00</option>
		      <option value="12">12:00</option>
	      </select>

      	  <select name="meridian">
			<?php
			  if ($meridian != "") {
			    if ($meridian == "%") {
			      $display_meridian = "";
			    } else {
			      $display_meridian = $meridian;
			    }
			    echo "<option value='$meridian' selected>$display_meridian</option>";
			  }
			?>
		      <option value="%"></option>
		      <option>AM</option>
		      <option>PM</option>
		  </select>
		  <br>
		  
		  <!-- select * from meetings where find_in_set('B',codes) > 0 and find_in_set('BB',codes) > 0; -->
		  <span class="label col1">Meeting Codes</span>
          <select name="codes[]" style="float:left;" multiple size=7>

				<?php
					$any_selected = "";
					$o_selected = "";
					$c_selected = "";
					$b_selected = "";
					$s_selected = "";
					$t_selected = "";
					$bb_selected = "";
					$d_selected = "";
					$od_selected = "";
					$cd_selected = "";
					$os_selected = "";
					$yp_selected = "";
					$g_selected = "";
					$w_selected = "";
					$mo_selected = "";
					$wo_selected = "";
					$df_selected = "";
					$cp_selected = "";
					$lm_selected = "";
					$sp_selected = "";
					$gv_selected = "";
					$a_selected = "";
					$ru_selected = "";
			
				   if (!empty($codes)) {
					   foreach ($codes as $code) {
					     if ($code == "%") { $any_selected = "selected"; }
					     if ($code == "O") { $o_selected = "selected"; }
					     if ($code == "C") { $c_selected = "selected"; }
					     if ($code == "B") { $b_selected = "selected"; }
					     if ($code == "S") { $s_selected = "selected"; }
					     if ($code == "T") { $t_selected = "selected"; }
					     if ($code == "BB") { $bb_selected = "selected"; }
					     if ($code == "D") { $d_selected = "selected"; }
					     if ($code == "OD") { $od_selected = "selected"; }
					     if ($code == "CD") { $cd_selected = "selected"; }
					     if ($code == "OS") { $os_selected = "selected"; }
					     if ($code == "YP") { $yp_selected = "selected"; }
					     if ($code == "G") { $g_selected = "selected"; }
					     if ($code == "HA") { $w_selected = "selected"; }
					     if ($code == "M") { $mo_selected = "selected"; }
					     if ($code == "W") { $wo_selected = "selected"; }
					     if ($code == "DF") { $df_selected = "selected"; }
					     if ($code == "CP") { $cp_selected = "selected"; }
					     if ($code == "LM") { $lm_selected = "selected"; }
					     if ($code == "SP") { $sp_selected = "selected"; }
					     if ($code == "GV") { $gv_selected = "selected"; }
					     if ($code == "A") { $a_selected = "selected"; }
					     if ($code == "RU") { $ru_selected = "selected"; }
					   }
				   }
				   	
				   if (empty($codes)) { $any_selected = "selected"; }
				?>
	
		        <option value="%" <?php echo $any_selected; ?>> Any </option>
		        <option value="O" <?php echo $o_selected; ?>> Open (O)</option>
		        <option value="C" <?php echo $c_selected; ?>> Closed (C)</option>
		        <option value="B" <?php echo $b_selected; ?>> Beginners (B)</option>
		        <option value="S" <?php echo $s_selected; ?>> Steps (S)</option>
		        <option value="T" <?php echo $t_selected; ?>> Traditions (T)</option>
		        <option value="BB" <?php echo $bb_selected; ?>> Big Book (BB)</option>
		        <option value="D" <?php echo $d_selected; ?>> Discussion (D)</option>
		        <option value="OD" <?php echo $od_selected; ?>> Open Discussion (OD)</option>
		        <option value="CD" <?php echo $cd_selected; ?>> Closed Discussion (CD)</option>
		        <option value="OS" <?php echo $os_selected; ?>> Open Speaker (OS)</option>
		        <option value="YP" <?php echo $yp_selected; ?>> Young People (YP)</option>
		        <option value="G" <?php echo $g_selected; ?>> Gay / All Welcome (G)</option>
		        <option value="HA" <?php echo $w_selected; ?>> Handicap Accessible (HA)</option>
		        <option value="M" <?php echo $mo_selected; ?>> Men's Meeting (M)</option>
		        <option value="W" <?php echo $wo_selected; ?>> Women's Meeting (W)</option>
		        <option value="DF" <?php echo $df_selected; ?>> Sign Language Interpreted (DF)</option>
		        <option value="CP" <?php echo $cp_selected; ?>> Couples (CP)</option>
		        <option value="LM" <?php echo $lm_selected; ?>> Lunch Meeting (LM)</option>
		        <option value="SP" <?php echo $sp_selected; ?>> Spanish (SP)</option>
		        <option value="GV" <?php echo $gv_selected; ?>> Grapevine (GV)</option>
		        <option value="A" <?php echo $a_selected; ?>> Al-Anon (A)</option>
		        <option value="RU" <?php echo $ru_selected; ?>> Russian (RU)</option>
			</select>
			<span class="ctlNote">(Hold CTRL key while clicking to add or remove codes)</span>
			<br>
			<br>
			<div class="submitBlock">
	      		<input type="submit" name="submit" value="Search">
  			</div>
		</div>
	</form>
</div>


<div style="clear:both">
<p>
	<?php
	if (isset($_POST['submit']) == "Search") {
		$i = 1;
		$return = $lookup->detailedSearch($location, addslashes($name), $city, $zip, $day, $time, $meridian, $codes);
	} elseif (isset($_POST['simple']) == "Search!") {
		$i = 1;
		$return = $lookup->simpleSearch($city,$zip);
	} else {
		echo "<center>Search fields are not required.  Leave search form blank to list all meetings; fill in select fields to narrow down the list.</center><br>\n";
	}

	if ($return != "") {
		if ($return[1] == 1) {
			$meetings = "meeting";
		} else {
			$meetings = "meetings";
		}

		if ($return[1] == 0) {
			echo "<center>No $meetings found. Please change your search criteria and try again.</center><br>\n";
		} else {
			echo "<center>$return[1] $meetings found. Click on meeting name for map and directions. Mouse over meeting codes for full descriptions.</center><br>\n";
		}

		echo "<table width=100% cellspacing=10>\n";
		$day_state = "";
		$query_result = $return[0];
		while ($row = $query_result->fetch_row())
		{
			if($i % 2)
			{
				$bgcolor = "#F4F4F4";
			}
			else {
				$bgcolor = "#FFFFFF";
			}

			if ($day_state != $row[9]) {
				echo "<tr bgcolor='#094EAA'>\n";
				echo "<td colspan=7><b><font color='#ffffff'>$row[9]</font></td>\n";
				echo "</tr>\n";
				//next;
			}

			echo "<tr bgcolor='$bgcolor'>\n";
			echo "<td><a href=\"editmeeting.php?row=$row[0]\"><img src=\"../images/edit_icon_small.png\" border=\"0\" alt=\"edit\" /></a></td>\n";
			echo "<td><a href=\"../map.php?row=$row[0]\">$row[10]</a></td>\n";
			echo "<td><a href=\"../map.php?row=$row[0]\">$row[6]</a></td>\n";
			echo "<td><a href=\"../map.php?row=$row[0]\">$row[7]</a></td>\n";

			$club = $row[2];
			if (strlen($club) > 18) {
				$club = substr($club, 0, 17) ."...";
			}

			$meet = $row[1];
			if (strlen($meet) > 18) {
				$meet = substr($meet, 0, 17) ."...";
			}

			echo "<td><a href=\"../map.php?row=$row[0]\">$club</a></td>\n";
			echo "<td><a href=\"../map.php?row=$row[0]\">$meet</a></td>\n";
			echo "<td><a href=\"javascript:\" style=\"text-decoration: none; font-weight: bold;\" onMouseOver=\"translateCodes('".$row[11]."','popup".$i."');\" id=\"popup".$i."\" title=\"\">$row[11]</a></td>\n";
			echo "</tr>\n";

			$city_state = $row[6];
			$zip_state = $row[7];
			$day_state = $row[9];
			$i++;
		}
		echo "</table>\n";
	}
	?>


</div>	

<?php include('../template/admin-footer.php'); ?>		

